package com.microservices.yucloud.appweb.exception;

/**
 * @author zy
 * @date 2019-08-30 14:59
 **/
public class BusinessException extends RuntimeException {

    private String code;
    private String message;

    public BusinessException(String code) {
        super();
        this.code = code;
    }

    public BusinessException(String code, String message) {
        super();
        this.code = code;
        this.message = message;
    }

    public BusinessException(String code, Throwable cause) {
        super(cause);
        this.code = code;
    }

    public BusinessException(String code, String message, Throwable cause) {
        super(cause);
        this.code = code;
        this.message = message;
    }

    @Override
    public String getMessage() {
        return message;
    }

    public static BusinessException handle(Throwable e, String code, String message) {
        if (e instanceof BusinessException){
            return (BusinessException) e;
        }
        if (message != null){
            return new BusinessException(code, message, e);
        }
        return new BusinessException(code, e);
    }
}
